import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios'
import { Toast,Dialog } 	from 'vant';
Vue.use(Toast,Dialog);

export default {
	name: 'moneyAgreement',
	data(){
		return {
            missionList:[], 
            showTotalNum:'',
            exchangeRate:{},
            openContract: false,
            currencySymbol: 'BTC',
            newGiftList: [],
            getNum: 0,
            giftList: [ 
                {
                    id: 0,
                    tit: '',
                    price: 5,
                    desc: '',
                    link: 'login',
                    target: 0,
                    typeN: 'register',
                    right: false,
                    state: false,
                },
                {
                    id: 1,
                    tit: '',
                    price: 5,
                    desc: '',
                    link: '',
                    target: 0,
                    typeN: '',
                    right: false,
                    state: false,
                },
                {
                    id: 2,
                    tit: '',
                    price: 5,
                    desc: '',
                    link: 'flutterApp://coinInOut',
                    target: 0,
                    typeN: 'recharge',
                    right: false,
                    state: false,
                },
                {
                    id: 3,
                    tit: '',
                    price: 25,
                    desc: '',
                    link: 'bbjApp://tradeHome',
                    target: 0,
                    typeN: 'recharge',
                    right: true,
                    state: false,
                },
                {
                    id: 4,
                    tit: '',
                    price: 60,
                    desc: '',
                    link: 'bbjApp://tradeHome',
                    target: 0,
                    typeN: 'recharge',
                    right: false,
                    state: false,
                },
                {
                    id: 5,
                    tit: '',
                    price: 10,
                    desc: '',
                    link: 'bbjApp://tradeHome',
                    target: 0,
                    typeN: 'trade',
                    right: false,
                    state: false,
                },
                {
                    id: 6,
                    tit: '',
                    price: 50,
                    desc: '',
                    link: 'bbjApp://tradeHome',
                    target: 0,
                    typeN: 'trade',
                    right: false,
                    state: false,
                },
            ],
            twitterLink: {
                'zh': 'https://twitter.com/Deepcoin_global',
                'en': 'https://twitter.com/Deepcoin_global',
                'ja': 'https://twitter.com/Deepcoin_JP'
            },
            helpRuleLink:{
                'zh': 'https://support.deepcoin.pro/hc/zh-cn/articles/360048184271-%E4%BD%93%E9%AA%8C%E9%87%91%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E',
                'en': 'https://support.deepcoin.pro/hc/en-001/categories/360003941211',
                'ja': 'https://support.deepcoin.pro/hc/ja/categories/360003941211'
            },
            followUrl: 'https://forms.gle/7MpRRAD1T6hnYL4a6'
		}
	},
	methods:{
		init(type){
            if(type) this.checkOpen()
            this.getMissionList()
        },
        //获取体验金金额信息
        getMissionList() {
            util.fetch(`${util.ajaxUrl3}/site/experience-mission`,{}).then(res => {
                let { retCode = -1, retData = []} = res || {}
                if(retCode == 0 && retData.length) {
                    this.missionList = retData
                    retData.map((item,ind) => {
                        if(this.giftList[ind]) {
                            this.giftList[ind].price = item.gift
                            this.giftList[ind].target = item.target
                            this.giftList[ind].type = item.type
                        }
                    })
                    if(this.token) {
                        this.missionProgress()
                    } else {
                        this.setTitleDesc()
                    }
                }
            })
        },
        
        //获取体验金对应状态
        missionProgress() {
            let _this = this
            util.fetch(`${util.ajaxUrl3}/experience-money/progress`,{token: this.token}).then(res => {
                let { retCode = -1, retData = []} = res || {}
                if(retCode == 0 && retData.length) {
                    this.giftList.map(item => (
                        retData.map(i => {
                            if(item.target == i.target && item.type == i.type) {
                                item.state = true
                            }
                        })
                    ))
                    this.setTitleDesc()
                } else {
                    this.setTitleDesc()
                }
            })
        },
        //获取对应汇率
        getCurrencyRate() {
            util.fetch(`${util.ajaxUrl3}/common/currency-rate`,{}).then(res => {
                this.exchangeRate = res.retData
                if(this.token) this.getAccountAny()
            })
        },
        //获取用户累计领取值
        getAccountAny() {
            util.fetch(`${util.ajaxUrl3}/experience-money/possess`,{token: this.token}).then(res => {
                // let { status, data } = res
                let { retCode = -1, retData } = res || {}
                if(retCode == 0) {
                    let thisVal = 0
                    if(this.locale == 'zh') thisVal = retData.rmb 
                    if(this.locale == 'ja') thisVal = this.exchangeRate.jpy * retData.rmb 
                    if(this.locale == 'en') thisVal = retData.usdt
                    thisVal = util.intercept(thisVal,2)
                    let f = this.locale != 'en' ? '￥' : '$'
                    this.showTotalNum = `${retData.amount_e8 || 0} (${this.$t('missionVal')} ${f}${thisVal || 0})`
                }
            })
        },
        //设置体验金对应文案
        setTitleDesc() {
            if(this.getNum < 1) {
                this.giftList.map((item,ind) => {
                    item.tit = `${this.$t('missionTitle'+ind)}`
                    if(ind == 1) {
                        item.desc = `${this.$t('missionDesc'+ind,{name: 'Twitter',code: 'Deepcoin_JP'})}`
                        item.link = this.twitterLink[this.locale]
                    } else if(ind > 1) {
                        item.desc = `${this.$t('missionDesc'+ind,{money: item.target})}`
                    } else {
                        item.desc = `${this.$t('missionDesc'+ind)}`
                    }
                })
                this.giftList = this.giftList.filter(i => i.type != 'official')
                this.newGiftList = this.giftList
                this.getNum++
            } else {
                this.getNum = 0
            }
        },
        junpFun(url) {
            util.callHandler('openUrl',{
				url: url,
				title:'',
				needLogin:false,
				popVC:false,		//跳转的时候是否干掉当前页面
                hiddenNav:false,	//是否全屏
                type: url.includes('tradeHome') ? 'inverse' : ''
			},res => {
				console.log(res)
			})
        },

        jump(url,flag) {
            if(flag) return
            console.log(url)
            if(this.token) {
                //openContract true 跳转到 开通合约
                if(this.openContract) {
                    this.junpFun('bbjApp://tradeHome')
                } else {
                    url && this.junpFun(url)
                }
            } else {
                //唤起登录
                util.callHandler('showLogin',{},res => {
                    console.log(res)
                })
            }
        },

        //校验是否开通合约
        checkOpen() {
            util.fetch(`${util.ajaxUrl3}/reverse-contract/check-open-reverse-contract`,{token: this.token}).then(res => {
                let { retCode = -1, retData } = res || {}
                this.openContract = retData.is_open == 0?true:false;
            })
        },
		goBack(){
			util.navigateBack(-1,this)
        },
        twitterClick() {
            util.callHandler('openUrl',{
                  url: this.twitterLink[`ja`], 
                  title: 'Deepcoin_JP',
                  needLogin: false
            })
        },
        flowoLink() {
            util.callHandler('openUrl',{
                url: this.followUrl, 
                title: 'Deepcoin_JP',
                needLogin: false
          })
        }
	},
	created() {
        this.getCurrencyRate()
		util.waitSDKReady().then(token => {
            this.token = token;
            if(this.token) this.checkOpen()
			this.init();
			setTimeout(() => {
				util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
					if(this.token) this.init(1);
					util.callHandler('setStatusBarStyle',{
						style:'light'
					})
				})
				//退出登录的时候native调用H5方法清除token缓存
				util.registerHandler('loginOutDeleteToken',res => {
					window.token = '';
					this.token = '';
					localStorage.removeItem('token');
				})
				util.callHandler('setStatusBarStyle',{
					style:'light'
				})
			},500)
		}).catch((e) => {
            let f = this.locale != 'en' ? '￥' : '$'
            this.showTotalNum = `0 (${this.$t('missionVal')} ${f}0)`
            this.init();
			// Toast('获取token失败')
		})
    }
}